from fastapi import APIRouter, Request
from fastapi import Depends
from requests import Session

from config.get_db import get_db
from core.entity.vo.common import CrudResponse
from core.entity.vo.view_models import CmsGaodeTraceVO
from core.service.cms_gaode_trace_service import CmsGaodeTraceService
from utils.log_util import *
from utils.response_util import *

gaodeTraceController = APIRouter()


@gaodeTraceController.post("/api/gaode_trace/query_by_id", response_model=CmsGaodeTraceVO)
async def get_gaode_trace_by_id(request: Request, id: int, query_db: Session = Depends(get_db)):
    try:
        # 获取全量数据
        cms_gaode_trace = CmsGaodeTraceService.get_cms_gaode_trace_by_id(query_db, id)
        logger.info('获取成功')
        return response_200(data=cms_gaode_trace, message="获取成功")
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))


@gaodeTraceController.post("/api/gaode_trace/query_by_trace_id", response_model=CmsGaodeTraceVO)
async def get_gaode_trace_by_trace_id(request: Request, trace_id: str, query_db: Session = Depends(get_db)):
    try:
        # 获取全量数据
        cms_gaode_trace = CmsGaodeTraceService.get_cms_gaode_trace_by_trace_id(query_db, trace_id)
        logger.info('获取成功')
        return response_200(data=cms_gaode_trace, message="获取成功")
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))


@gaodeTraceController.post("/gaode_trace/query_like_trace_name", response_model=CmsGaodeTraceVO)
async def get_gaode_trace_like_trace_name(request: Request, trace_name: str, query_db: Session = Depends(get_db)):
    try:
        # 获取全量数据
        cms_gaode_trace_list = CmsGaodeTraceService.get_cms_gaode_trace_like_name(query_db, trace_name)
        logger.info('获取成功')
        return response_200(data=cms_gaode_trace_list, message="获取成功")
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))


@gaodeTraceController.post("/gaode_trace/add", response_model=CrudResponse)
async def add_gaode_trace(request: Request, cms_gaode_trace: CmsGaodeTraceVO, query_db: Session = Depends(get_db)):
    try:
        cms_gaode_trace_result = await CmsGaodeTraceService.add_cms_gaode_trace(request, query_db, cms_gaode_trace)
        if cms_gaode_trace_result.is_success:
            logger.info(cms_gaode_trace_result.message)
            return response_200(data=cms_gaode_trace_result, message=cms_gaode_trace_result.message)
        else:
            logger.warning(cms_gaode_trace_result.message)
            return response_400(data="", message=cms_gaode_trace_result.message)
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))


@gaodeTraceController.post("/gaode_trace/search", response_model=CrudResponse)
async def search_gaode_trace(request: Request, cms_gaode_trace: CmsGaodeTraceVO, query_db: Session = Depends(get_db)):
    try:
        cms_gaode_trace_result = await CmsGaodeTraceService.search_cms_gaode_trace(request, query_db, cms_gaode_trace)
        if cms_gaode_trace_result.is_success:
            logger.info(cms_gaode_trace_result.message)
            return response_200(data=cms_gaode_trace_result, message=cms_gaode_trace_result.message)
        else:
            logger.warning(cms_gaode_trace_result.message)
            return response_400(data="", message=cms_gaode_trace_result.message)
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))

@gaodeTraceController.post("/gaode_trace/delete", response_model=CrudResponse)
async def delete_gaode_trace(request: Request, cms_gaode_trace: CmsGaodeTraceVO, query_db: Session = Depends(get_db)):
    try:
        delete_gaode_trace_result = CmsGaodeTraceService.delete_cms_gaode_trace(query_db, cms_gaode_trace.id,
                                                                                cms_gaode_trace.service_id,
                                                                                cms_gaode_trace.terminal_id,
                                                                                cms_gaode_trace.trace_id)
        if delete_gaode_trace_result.is_success:
            logger.info(delete_gaode_trace_result.message)
            return response_200(data=delete_gaode_trace_result, message=delete_gaode_trace_result.message)
        else:
            logger.warning(delete_gaode_trace_result.message)
            return response_400(data="", message=delete_gaode_trace_result.message)
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))
